import React, { PureComponent } from 'react';
import { Form, Input, notification } from 'antd';
import { connect } from 'dva';
import "./form.less"
class ComponentProps extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            showForm:true
        }
        this.onConfirm=this.onConfirm.bind(this)
        this.resetForm=this.resetForm.bind(this)
        this.getDictName=this.getDictName.bind(this)
    }
    componentDidMount() {
        const {dictlist,onLoaded}=this.props;
        if (onLoaded) {
           onLoaded(this)
        }
      
        this.setState({
            unitlist: dictlist.filter(item => item.dictType == "unit"),
        })

    }
    reset() {
        let { form } = this.props;
        form.resetFields()
    }
    onConfirm() {
        return new Promise((resolve, reject) => {
            let { form } = this.props;
            form.validateFields((error, values) => {
                if(!error){
                    resolve()
                }else{
                    reject()
                }
            })
        })
    }
    getDictName(list,attrEnName){
        let obj=list.find(item=>item.id==attrEnName);
        return obj?obj.dictLabel:""
    }
    async resetForm(){
        await this.setState({
            showForm:false
        })
        this.setState({
            showForm:true
        })
    }
    render() {
        const { mode, data } = this.props
        const {showForm,unitlist}=this.state;
        const { getFieldDecorator } = this.props.form
        return (
            showForm?<Form
            labelAlign="right"
                labelCol={{ span:10 }}
                wrapperCol={{ span: 14 }}
            >
                {
                    data && data.length ? data.map((item, index) => {
                        return (
             
                                <Form.Item style={{margin:"0"}} className="half" key={'prop' + index} label={item.name+'('+this.getDictName(unitlist,item.unit)+')'}>
                                    {getFieldDecorator(item.key, {
                                        rules: [
                                            { required: true, message: '必填' }
                                        ],
                                        initialValue: item.value
                                    })(
                                        <Input onChange={e => {
                                            e.persist();
                                            this.props.onChange(item, e.target.value)
                                        }} />
                                    )}
                                </Form.Item>
                        )
                    }) : undefined
                   
                }
                 <div className="half"></div>

            </Form>:null
        )
    }
}
export default connect(
    ({
        dict:{dictlist}
    }) => ({
        dictlist
    }),
    dispatch => ({

    })
)(Form.create('ComponentProps', {

})(ComponentProps));
